package com.ruoyi.system.controller;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.util.DownloadService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Cover;
import com.ruoyi.system.service.ICoverService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 井盖管理Controller
 * 
 * @author ruoyi
 * @date 2023-07-04
 */
@Api("井盖管理")
@RestController
@RequestMapping("/system/cover")
public class CoverController extends BaseController
{
    @Autowired
    private ICoverService coverService;

    @Autowired
    private DownloadService downloadService;


    /**
     * 查询井盖管理列表
     */
    @ApiOperation("查询井盖管理列表")
    @PreAuthorize("@ss.hasPermi('system:cover:list')")
    @GetMapping("/list")
    public TableDataInfo list(Cover cover)
    {
        startPage();
        List<Cover> list = coverService.selectCoverList(cover);
        return getDataTable(list);
    }

    @GetMapping("/getCode")
    public TableDataInfo CodeList(){
        List<Cover> list =  coverService.selectCodeList();
        return getDataTable(list);
    }

    /**
     * 导出井盖管理列表
     */
    @ApiOperation("导出井盖管理列表")
    @PreAuthorize("@ss.hasPermi('system:cover:export')")
    @Log(title = "井盖管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Cover cover)
    {
        List<Cover> list = coverService.selectCoverList(cover);
        ExcelUtil<Cover> util = new ExcelUtil<Cover>(Cover.class);
        util.exportExcel(response, list, "井盖管理数据");
    }

    /**
     * 获取井盖管理详细信息
     */
    @ApiOperation("获取井盖管理详细信息")
    @PreAuthorize("@ss.hasPermi('system:cover:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(coverService.selectCoverById(id));
    }

    /**
     * 新增井盖管理
     */
    @ApiOperation("新增井盖管理")
    @PreAuthorize("@ss.hasPermi('system:cover:add')")
    @Log(title = "井盖管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Cover cover) {
        List<Cover> list =  coverService.selectCodeList();
        List<String> list1=new ArrayList<>();
        for (Cover covers : list) {
            list1.add(covers.getCode());
        }

        if (!list1.contains(cover.getCode())){
            return toAjax(coverService.insertCover(cover));
        }else {
            return error("井盖编码不能重复");
        }
    }

    /**
     * 修改井盖管理
     */
    @ApiOperation("修改井盖管理")
    @PreAuthorize("@ss.hasPermi('system:cover:edit')")
    @Log(title = "井盖管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Cover cover)
    {
        return toAjax(coverService.updateCover(cover));
    }

    /**
     * 删除井盖管理
     */
    @ApiOperation("删除井盖管理")
    @PreAuthorize("@ss.hasPermi('system:cover:remove')")
    @Log(title = "井盖管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long ids)
    {
        return toAjax(coverService.deleteCoverById(ids));
    }

    @ApiOperation("井盖导入")
    @Log(title = "井盖导入", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('system:cover:import')")
    @PostMapping("/importData")
    public AjaxResult importData(@RequestParam("file") MultipartFile file, String region, String flats) {
        //查询编码
        List<Cover> list =  coverService.selectCodeList();
        List<String> list1=new ArrayList<>();
        for (Cover cover : list) {
            list1.add(cover.getCode());
        }
        try{
            ExcelUtil<Cover> util = new ExcelUtil<>(Cover.class);
            List<Cover> coverList = util.importExcel(file.getInputStream());
            for (int i = 0; i < coverList.size(); i++) {
                if (!list1.contains(coverList.get(i).getCode())){
                    coverService.insertCoverExcel(coverList.get(i).getCoverType(),coverList.get(i).getCoverUsefor(),coverList.get(i).getCode(),coverList.get(i).getAddress(),region,flats);
                }else {
                    return error("导入的excel文件中井盖编码有重复的，请重写导入");
                }
            }
            return success("上传成功");
        } catch (Exception e) {
            return error("上传失败");
        }
    }

    /**
     * 下载模板
     * @param response
     */
    @ApiOperation("下载井盖模版")
    @PreAuthorize("@ss.hasPermi('system:cover:downtemplate')")
    @PostMapping ("/downInChargeOfTemplate")
    public void downInChargeOfTemplate(HttpServletResponse response) {
        downloadService.downInChargeOfTemplate(response);
    }

}
